package controllerCollection;

import java.awt.event.ActionListener;
import java.util.Vector;

import dataCollection.Dice;

import viewCollection.CardTraitDialog;
import viewCollection.View;
import modelCollection.Model;

import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import dataCollection.Card;
import dataCollection.Card.Value;
import viewCollection.TraitsView.TraitHandler;

public class ControllTraits {
  
  private Model model_;
  private View view_;
  public ControllTraits(Model model, View view) {
    model_ = model;
    view_ = view;
    view_.setTraitButtonListener(new TraitsListener());
    for(TraitHandler trait : view_.getTraitsView().getTraits()) {
      trait.button_.addActionListener(new TraitsButtonListener(trait));
    }
  }
  /*
   * 
   */
  class TraitsListener implements ActionListener {
    boolean is_active_;
    public TraitsListener() {
      is_active_ = false;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
      if(!is_active_) {
        is_active_ = true;
        Vector<Dice> dice_vec = new Vector<Dice>();
        CardTraitDialog d = new CardTraitDialog();
        d.table_.addMouseListener(new CardTraitMouseListener(d));
        d.delete_button_.addActionListener(new CardTraitDeleteButtonListener(d));
        d.ok_button_.addActionListener(new CardTraitOKButtonListener(d.table_, dice_vec, d));
        d.setVisible(true);
        view_.paintTraits(dice_vec);
      }
    }
  }
  
  /*
   * 
   */
  class TraitsButtonListener implements ActionListener {
    private TraitHandler trait_;
    TraitsButtonListener(TraitHandler trait) {
      trait_ = trait;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
      String trait_name = trait_.name_;
      Dice dice = model_.getChar().getTraitDice(trait_name);
      if(dice == null) {
        dice = trait_.getSelectedItem();
        if(dice== null) return;
        System.out.println("Dice name: " + dice.toString());
        model_.getChar().setTrait(trait_.name_, dice);
        
        // disable OK Button and ComboBox; remove Dice
        trait_.removeElement(dice);
        model_.getChar().printChar(); 
      } else {
        System.out.println("RESTORE");
        trait_.restoreElement(dice);
        model_.getChar().removeTraitDice(trait_name);
      }
      
    }
  }
  
  class CardTraitDeleteButtonListener implements ActionListener {
    private CardTraitDialog dialog_;
    CardTraitDeleteButtonListener(CardTraitDialog dialog) {
      dialog_ = dialog;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
      JTable table = dialog_.table_;
      if(table.getSelectedRow() < 0) return; 
      ((DefaultTableModel)table.getModel()).removeRow(table.getSelectedRow());
      if(table.getRowCount() <= 10) {
        dialog_.setAllowToClose(true);
        dialog_.ok_button_.setEnabled(dialog_.getAllowToClose());
        dialog_.delete_button_.setEnabled(!dialog_.getAllowToClose());
      }
    }
  } 
  
  class CardTraitOKButtonListener implements ActionListener {
    private JTable table_;
    private Vector<Dice> dice_vec_;
    private CardTraitDialog d_;
    CardTraitOKButtonListener(JTable table, Vector<Dice> dice_vec, CardTraitDialog d) {
      table_ = table;
      dice_vec_ = dice_vec;
      d_ = d;
    }
    
    @Override
    public void actionPerformed(ActionEvent e){
      System.out.println(table_.getRowCount());
      for(int i = 0; i < table_.getRowCount(); i++) {
        DefaultTableModel model = (DefaultTableModel)table_.getModel();
        Dice d = (Dice) model.getValueAt(i, 1);
        if(d == null && 
           ((Card)model.getValueAt(i, 0)).getValue().equals(Value.JOKER)) {
          d = (Dice) table_.getCellEditor(i, 1).getCellEditorValue();
          if(d == null) {
            dice_vec_.clear();
            return;
          }
        }
        dice_vec_.add(d);
      }
      d_.setVisible(false);
    }
  } 

  class CardTraitMouseListener implements MouseListener {
    private CardTraitDialog dialog_;
    public CardTraitMouseListener(CardTraitDialog dialog) {
      dialog_ = dialog;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
      System.out.println("Clicked");
      System.out.println("allow to close" + dialog_.getAllowToClose());
      DefaultTableModel model = (DefaultTableModel)dialog_.table_.getModel();
      Card c = (Card)model.getValueAt(dialog_.table_.getSelectedRow(), 0);
      if(c.getValue() == Card.Value.JOKER ||
         c.getValue() == Card.Value.TWO) {
        dialog_.delete_button_.setEnabled(false);
      } else {
        dialog_.delete_button_.setEnabled(!dialog_.getAllowToClose());
      }
    }

    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    
    public void mouseReleased(MouseEvent e) {
      DefaultTableModel model = (DefaultTableModel)dialog_.table_.getModel();
      Card c = (Card)model.getValueAt(dialog_.table_.getSelectedRow(), 0);
      if(dialog_.table_.getSelectedRowCount() > 1) {
        dialog_.delete_button_.setEnabled(false);
      }  else if(c.getValue() == Card.Value.JOKER ||
         c.getValue() == Card.Value.TWO) {
        dialog_.delete_button_.setEnabled(false);
      } else {
        dialog_.delete_button_.setEnabled(!dialog_.getAllowToClose());
      }    }
  }
}
